package top.market.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class LyhStringRedisTemplate {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void setObjStr(Object obj1, Object obj2, Long time, TimeUnit unit){
        stringRedisTemplate.opsForValue().set(JSON.toJSONString(obj1), JSON.toJSONString(obj2), time, unit);
    }

    public void setObjStr(Object obj1, Object obj2){
        stringRedisTemplate.opsForValue().set(JSON.toJSONString(obj1), JSON.toJSONString(obj2));
    }

    public <T> T getObjStr(Object obj, Class<T> clazz){
        String s = stringRedisTemplate.opsForValue().get(JSON.toJSONString(obj));
        return JSONObject.parseObject(s, clazz);
    }

    public <T> List<T> getObjArrayStr(Object obj, Class<T> clazz){
        String s = stringRedisTemplate.opsForValue().get(JSON.toJSONString(obj));
        return JSONObject.parseArray(s, clazz);
    }

    public Boolean delete(String key) {
        return  stringRedisTemplate.delete(JSON.toJSONString(key));
    }

    public Boolean expire(String key, long time, TimeUnit unit){
        return stringRedisTemplate.expire(key, time, unit);
    }
}
